package com.tingyuge.thinking.spring.configuration.bean.definition;

import com.tingyuge.thinking.spring.ioc.overview.domain.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;

/**
 * {@link org.springframework.beans.factory.config.BeanDefinition} 构建示例
 * @author xiaodao
 * @version 1.0
 * @date 2021/6/15
 */
public class BeanDefinitionCreationDemo {

    public static void main(String[] args) {
        //1.通过BeanDefinitionBuilder
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
        //通过属性设置
        beanDefinitionBuilder.addPropertyValue("id", 44).addPropertyValue("name", "小刀");
        //获取beanDefinition 实例
        AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
        //beanDefinition并非Bean最终态,可以自定义修改

        //2.通过AbstractBeanDefinition 以及派生类
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        //设置Bean 类型
        genericBeanDefinition.setBeanClass(User.class);

        //通过MutablePropertyValues 批量设置属性
        MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
        mutablePropertyValues.add("id",1);
        mutablePropertyValues.add("name","小刀");
        genericBeanDefinition.setPropertyValues(mutablePropertyValues);
    }
}
